package core.views;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import core.Runner;
import core.models.items.*;

/**
 * Displays the store.
 * 
 * @author 4c1a
 */
public class StorePanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField moneyTextField = new JTextField();
	private JTextField bulletFieldStatic;
	private JTextField clothingFieldStatic;
	private JTextField foodFieldStatic;
	private JTextField gunFieldStatic;
	private JTextField medicineFieldStatic;
	private JTextField wAxleFieldStatic;
	private JTextField wTongueFieldStatic;
	private JTextField wWheelFieldStatic;
	private JTextField bulletFieldEdit;
	private JTextField clothingFieldEdit;
	private JTextField foodFieldEdit;
	private JTextField gunFieldEdit;
	private JTextField medicineFieldEdit;
	private JTextField wAxleFieldEdit;
	private JTextField wTongueFieldEdit;
	private JTextField wWheelFieldEdit;
	private JTextField bulletFieldPer;
	private JTextField clothingFieldPer;
	private JTextField foodFieldPer;
	private JTextField gunFieldPer;
	private JTextField medicineFieldPer;
	private JTextField wTongueFieldPer;
	private JTextField wAxleFieldPer;
	private JTextField wWheelFieldPer;
	private Runner runner;
	private final JLabel label_8 = new JLabel("$");
	private JTextField oxenFieldEdit;
	private JTextField oxenFieldPer;
	private JTextField oxenFieldStatic;

	/**
	 * Brings up a screen that allows the user to purchase items such as
	 * bullets, clothes, food, a gun, medicine, wagon axles, wagon tongues,
	 * wagon wheels, and oxen. The screen also displays the player's available
	 * money.
	 * 
	 * @param runner The runner to be used by the panel.
	 */
	public StorePanel(Runner runner) {
		this.runner = runner;
		setLayout(null);

		JLabel lblBullet = new JLabel("Bullet");
		lblBullet.setBounds(10, 36, 46, 14);
		add(lblBullet);

		JLabel lblClothing = new JLabel("Clothing");
		lblClothing.setBounds(10, 61, 46, 14);
		add(lblClothing);

		JLabel label = new JLabel("Food");
		label.setBounds(10, 86, 46, 14);
		add(label);

		JLabel label_1 = new JLabel("Gun");
		label_1.setBounds(10, 115, 46, 14);
		add(label_1);

		JLabel label_2 = new JLabel("Medicine");
		label_2.setBounds(10, 140, 63, 14);
		add(label_2);

		JLabel label_3 = new JLabel("WagonAxle");
		label_3.setBounds(10, 165, 99, 14);
		add(label_3);

		JLabel label_4 = new JLabel("WagonTongue");
		label_4.setBounds(10, 190, 99, 14);
		add(label_4);

		JLabel label_5 = new JLabel("WagonWheel");
		label_5.setBounds(10, 215, 99, 14);
		add(label_5);

		JLabel lblAvailableMoney = new JLabel("Available Money:");
		lblAvailableMoney.setBounds(10, 271, 99, 14);
		add(lblAvailableMoney);
		moneyTextField.setEditable(false);
		moneyTextField.setBounds(136, 268, 40, 19);
		add(moneyTextField);
		moneyTextField.setColumns(10);

		JLabel label_6 = new JLabel("total price");
		label_6.setBounds(288, 11, 92, 14);
		add(label_6);

		bulletFieldStatic = new JTextField();
		bulletFieldStatic.setText("0");
		bulletFieldStatic.setEditable(false);
		bulletFieldStatic.setColumns(10);
		bulletFieldStatic.setBounds(305, 36, 44, 17);
		add(bulletFieldStatic);

		clothingFieldStatic = new JTextField();
		clothingFieldStatic.setText("0");
		clothingFieldStatic.setEditable(false);
		clothingFieldStatic.setColumns(10);
		clothingFieldStatic.setBounds(305, 61, 44, 17);
		add(clothingFieldStatic);

		foodFieldStatic = new JTextField();
		foodFieldStatic.setText("0");
		foodFieldStatic.setEditable(false);
		foodFieldStatic.setColumns(10);
		foodFieldStatic.setBounds(305, 86, 44, 17);
		add(foodFieldStatic);

		gunFieldStatic = new JTextField();
		gunFieldStatic.setText("0");
		gunFieldStatic.setEditable(false);
		gunFieldStatic.setColumns(10);
		gunFieldStatic.setBounds(305, 115, 44, 17);
		add(gunFieldStatic);

		medicineFieldStatic = new JTextField();
		medicineFieldStatic.setText("0");
		medicineFieldStatic.setEditable(false);
		medicineFieldStatic.setColumns(10);
		medicineFieldStatic.setBounds(305, 140, 44, 17);
		add(medicineFieldStatic);

		wAxleFieldStatic = new JTextField();
		wAxleFieldStatic.setText("0");
		wAxleFieldStatic.setEditable(false);
		wAxleFieldStatic.setColumns(10);
		wAxleFieldStatic.setBounds(305, 165, 44, 17);
		add(wAxleFieldStatic);

		wTongueFieldStatic = new JTextField();
		wTongueFieldStatic.setText("0");
		wTongueFieldStatic.setEditable(false);
		wTongueFieldStatic.setColumns(10);
		wTongueFieldStatic.setBounds(305, 190, 44, 17);
		add(wTongueFieldStatic);

		wWheelFieldStatic = new JTextField();
		wWheelFieldStatic.setText("0");
		wWheelFieldStatic.setEditable(false);
		wWheelFieldStatic.setColumns(10);
		wWheelFieldStatic.setBounds(305, 213, 44, 17);
		add(wWheelFieldStatic);

		bulletFieldEdit = new JTextField();
		bulletFieldEdit.setBounds(136, 33, 40, 17);
		add(bulletFieldEdit);
		bulletFieldEdit.setColumns(10);
		bulletFieldEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try	{
				if(Integer.parseInt(bulletFieldEdit.getText())<0)
				{
					bulletFieldEdit.setText("");
				}
				String tempText = ""
						+ (Integer.parseInt(bulletFieldEdit.getText()) * Integer
								.parseInt(bulletFieldPer.getText()));
				bulletFieldStatic.setText(tempText);
				}
				catch(NumberFormatException ex)
				{
					bulletFieldEdit.setText("");
				}
			}
		});

		clothingFieldEdit = new JTextField();
		clothingFieldEdit.setColumns(10);
		clothingFieldEdit.setBounds(136, 58, 40, 17);
		add(clothingFieldEdit);
		clothingFieldEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					if(Integer.parseInt(clothingFieldEdit.getText())<0)
					{
						clothingFieldEdit.setText("");
					}
					String tempText = ""
							+ (Integer.parseInt(clothingFieldEdit.getText()) * Integer
									.parseInt(clothingFieldPer.getText()));
					clothingFieldStatic.setText(tempText);
				}
				catch(NumberFormatException ex)
				{
					clothingFieldEdit.setText("");
				}
			}
		});

		foodFieldEdit = new JTextField();
		foodFieldEdit.setColumns(10);
		foodFieldEdit.setBounds(136, 83, 40, 17);
		add(foodFieldEdit);
		foodFieldEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					if(Integer.parseInt(foodFieldEdit.getText())<0)
					{
						foodFieldEdit.setText("");
					}
					String tempText = ""
							+ (Integer.parseInt(foodFieldEdit.getText()) * Integer
									.parseInt(foodFieldPer.getText()));
					foodFieldStatic.setText(tempText);
				}
				catch(NumberFormatException ex)
				{
					foodFieldEdit.setText("");
				}
			}
		});

		gunFieldEdit = new JTextField();
		gunFieldEdit.setColumns(10);
		gunFieldEdit.setBounds(136, 112, 40, 17);
		add(gunFieldEdit);
		gunFieldEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					if(Integer.parseInt(gunFieldEdit.getText())<0)
					{
						gunFieldEdit.setText("");
					}
					String tempText = ""
							+ (Integer.parseInt(gunFieldEdit.getText()) * Integer
									.parseInt(gunFieldPer.getText()));
					gunFieldStatic.setText(tempText);
				}
				catch(NumberFormatException ex)
				{
					gunFieldEdit.setText("");
				}
			}
		});

		medicineFieldEdit = new JTextField();
		medicineFieldEdit.setColumns(10);
		medicineFieldEdit.setBounds(136, 137, 40, 17);
		add(medicineFieldEdit);
		medicineFieldEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					if(Integer.parseInt(medicineFieldEdit.getText())<0)
					{
						medicineFieldEdit.setText("");
					}
					String tempText = ""
							+ (Integer.parseInt(medicineFieldEdit.getText()) * Integer
									.parseInt(medicineFieldPer.getText()));
					medicineFieldStatic.setText(tempText);
				}
				catch(NumberFormatException ex)
				{
					medicineFieldEdit.setText("");
				}
			}
		});

		wAxleFieldEdit = new JTextField();
		wAxleFieldEdit.setColumns(10);
		wAxleFieldEdit.setBounds(136, 162, 40, 17);
		add(wAxleFieldEdit);
		wAxleFieldEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					if(Integer.parseInt(wAxleFieldEdit.getText())<0)
					{
						wAxleFieldEdit.setText("");
					}
					String tempText = ""
							+ (Integer.parseInt(wAxleFieldEdit.getText()) * Integer
									.parseInt(wAxleFieldPer.getText()));
					wAxleFieldStatic.setText(tempText);
				}
				catch(NumberFormatException ex)
				{
					wAxleFieldEdit.setText("");
				}
			}
		});

		wTongueFieldEdit = new JTextField();
		wTongueFieldEdit.setColumns(10);
		wTongueFieldEdit.setBounds(136, 187, 40, 17);
		add(wTongueFieldEdit);
		wTongueFieldEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					if(Integer.parseInt(wTongueFieldEdit.getText())<0)
					{
						wTongueFieldEdit.setText("");
					}
					String tempText = ""
							+ (Integer.parseInt(wTongueFieldEdit.getText()) * Integer
									.parseInt(wTongueFieldPer.getText()));
					wTongueFieldStatic.setText(tempText);
				}
				catch(NumberFormatException ex)
				{
					wTongueFieldEdit.setText("");
				}
			}
		});

		wWheelFieldEdit = new JTextField();
		wWheelFieldEdit.setColumns(10);
		wWheelFieldEdit.setBounds(136, 212, 40, 17);
		add(wWheelFieldEdit);
		
		oxenFieldEdit = new JTextField();
		oxenFieldEdit.setBounds(136, 237, 40, 20);
		add(oxenFieldEdit);
		oxenFieldEdit.setColumns(10);
		oxenFieldEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					if(Integer.parseInt(oxenFieldEdit.getText())<0)
					{
						oxenFieldEdit.setText("");
					}
					String tempText = ""
							+ (Integer.parseInt(oxenFieldEdit.getText()) * Integer
									.parseInt(oxenFieldPer.getText()));
					oxenFieldStatic.setText(tempText);
				}
				catch(NumberFormatException ex)
				{
					oxenFieldEdit.setText("");
				}
			}
		});

		JLabel label_7 = new JLabel("Price per item");
		label_7.setBounds(203, 11, 92, 14);
		add(label_7);

		bulletFieldPer = new JTextField();
		bulletFieldPer.setEditable(false);
		bulletFieldPer.setColumns(10);
		bulletFieldPer.setBounds(221, 36, 44, 17);
		add(bulletFieldPer);

		clothingFieldPer = new JTextField();
		clothingFieldPer.setEditable(false);
		clothingFieldPer.setColumns(10);
		clothingFieldPer.setBounds(221, 61, 44, 17);
		add(clothingFieldPer);

		foodFieldPer = new JTextField();
		foodFieldPer.setEditable(false);
		foodFieldPer.setColumns(10);
		foodFieldPer.setBounds(221, 86, 44, 17);
		add(foodFieldPer);

		gunFieldPer = new JTextField();
		gunFieldPer.setEditable(false);
		gunFieldPer.setColumns(10);
		gunFieldPer.setBounds(221, 115, 44, 17);
		add(gunFieldPer);

		medicineFieldPer = new JTextField();
		medicineFieldPer.setEditable(false);
		medicineFieldPer.setColumns(10);
		medicineFieldPer.setBounds(221, 140, 44, 17);
		add(medicineFieldPer);

		wTongueFieldPer = new JTextField();
		wTongueFieldPer.setEditable(false);
		wTongueFieldPer.setColumns(10);
		wTongueFieldPer.setBounds(221, 190, 44, 17);
		add(wTongueFieldPer);

		wAxleFieldPer = new JTextField();
		wAxleFieldPer.setEditable(false);
		wAxleFieldPer.setColumns(10);
		wAxleFieldPer.setBounds(221, 165, 44, 17);
		add(wAxleFieldPer);

		wWheelFieldPer = new JTextField();
		wWheelFieldPer.setEditable(false);
		wWheelFieldPer.setColumns(10);
		wWheelFieldPer.setBounds(221, 212, 44, 17);
		add(wWheelFieldPer);
		label_8.setBounds(119, 267, 14, 23);
		add(label_8);
		
		JLabel lblOxen = new JLabel("Oxen");
		lblOxen.setBounds(10, 240, 46, 14);
		add(lblOxen);
		
		
		
		oxenFieldPer = new JTextField();
		oxenFieldPer.setEditable(false);
		oxenFieldPer.setBounds(221, 237, 44, 17);
		add(oxenFieldPer);
		oxenFieldPer.setColumns(10);
		
		oxenFieldStatic = new JTextField();
		oxenFieldStatic.setEditable(false);
		oxenFieldStatic.setText("0");
		oxenFieldStatic.setBounds(305, 237, 44, 19);
		add(oxenFieldStatic);
		oxenFieldStatic.setColumns(10);
		loadItemsPrice();

		wWheelFieldEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					if(Integer.parseInt(wWheelFieldEdit.getText())<0)
					{
						wWheelFieldEdit.setText("");
					}
					String tempText = ""
							+ (Integer.parseInt(wWheelFieldEdit.getText()) * Integer
									.parseInt(wWheelFieldPer.getText()));
					wWheelFieldStatic.setText(tempText);
				}
				catch(NumberFormatException ex)
				{
					wWheelFieldStatic.setText("");
				}
			}
		});
		this.update();
	}
	
	/**
	 * Helper method for getting the price of the items.
	 * 
	 */
	public void loadItemsPrice() {
		List<Item> itemList = runner.getStore().getInventory();
		int money = runner.getGroup().getMoney();
		moneyTextField.setText(Integer.toString(money));

		for (Item item : itemList) {
			String name = item.getName();
			String price = Integer.toString(item.getPrice());
			if (name == "Wagon Wheel")
				wWheelFieldPer.setText(price);
			else if (name == "Wagon Axle")
				wAxleFieldPer.setText(price);
			else if (name == "Medicine")
				medicineFieldPer.setText(price);
			else if (name == "Wagon Tongue")
				wTongueFieldPer.setText(price);
			else if (name == "Guns")
				gunFieldPer.setText(price);
			else if (name == "Food")
				foodFieldPer.setText(price);
			else if (name == "Clothing")
				clothingFieldPer.setText(price);
			else if (name == "Bullets")
				bulletFieldPer.setText(price);
			else if (name == "Oxen")
				oxenFieldPer.setText(price);
		}
	}

	/**
	 * Gets the weight of the items that have been purchased.
	 * 
	 * @param items The list of items to be processed.
	 * 
	 * @return The total weight of the items in the list.
	 */
	private int getListWeight(List<Item> items) {
		int weight = 0;
		for (int i = 0; i < items.size(); i++) {
			weight += (items.get(i).getQuantity() * items.get(i).getWeight());
		}
		return weight;
	}

	/**
	 * Returns the cost of everything
	 * 
	 * @return The total cost of everything purchased
	 */
	private int getTotalCost() {
		int totalCost = 0;
		try{
		totalCost += Integer.parseInt(bulletFieldStatic.getText());
		}
		catch(NumberFormatException nfe)
		{
			totalCost+=0;
			bulletFieldStatic.setText("");
		}
		totalCost += Integer.parseInt(clothingFieldStatic.getText());
		totalCost += Integer.parseInt(foodFieldStatic.getText());
		totalCost += Integer.parseInt(gunFieldStatic.getText());
		totalCost += Integer.parseInt(medicineFieldStatic.getText());
		totalCost += Integer.parseInt(wAxleFieldStatic.getText());
		totalCost += Integer.parseInt(wTongueFieldStatic.getText());
		totalCost += Integer.parseInt(wWheelFieldStatic.getText());
		totalCost += Integer.parseInt(oxenFieldStatic.getText());
		return totalCost;
	}

	/**
	 * Updates the wagon's inventory with recently purchased items.
	 */
	public void update() {
		updateBuy();
		int totalCost = getTotalCost();

		if (totalCost > Integer.parseInt(moneyTextField.getText())) {

			JFrame x = new JFrame();
			JOptionPane.showMessageDialog(x, "Not Enough Money!");
		} else {
			List<Item> toBuy = new ArrayList<Item>();
			
			if (!(bulletFieldEdit.getText().equals(""))) {
				try{
				Bullet b = new Bullet(Integer.parseInt(bulletFieldEdit
						.getText()));
				toBuy.add(b);
				}
				catch(NumberFormatException nfe)
				{
					bulletFieldEdit.setText("");
				}

			}
			if (!(clothingFieldEdit.getText().equals(""))) {
				Clothing c = new Clothing(Integer.parseInt(clothingFieldEdit
						.getText()));
				toBuy.add(c);
			}
			if (!(foodFieldEdit.getText().equals(""))) {
				Food f = new Food(Integer.parseInt(foodFieldEdit.getText()));
				toBuy.add(f);
			}
			if (!(gunFieldEdit.getText().equals(""))) {
				Gun g = new Gun(Integer.parseInt(gunFieldEdit.getText()));
				toBuy.add(g);
			}
			if (!(medicineFieldEdit.getText().equals(""))) {
				Medicine m = new Medicine(Integer.parseInt(medicineFieldEdit
						.getText()));
				toBuy.add(m);
			}
			if (!(wAxleFieldEdit.getText().equals(""))) {
				WagonAxle wa = new WagonAxle(Integer.parseInt(wAxleFieldEdit
						.getText()));
				toBuy.add(wa);
			}
			if (!(wTongueFieldEdit.getText().equals(""))) {
				WagonTongue wt = new WagonTongue(
						Integer.parseInt(wTongueFieldEdit.getText()));
				toBuy.add(wt);
			}
			if (!(wWheelFieldEdit.getText().equals(""))) {
				WagonWheel ww = new WagonWheel(Integer.parseInt(wWheelFieldEdit
						.getText()));
				toBuy.add(ww);
			}
			if (!(oxenFieldEdit.getText().equals(""))) {
				Oxen o = new Oxen(Integer.parseInt(oxenFieldEdit
						.getText()));
				toBuy.add(o);
			}
			int weightTotal = getListWeight(toBuy);

			if (weightTotal > runner.getGroup().getWagon().getRemainingWeight()) {
				JFrame y = new JFrame();
				JOptionPane
						.showMessageDialog(y, "Not Enough Weight Remaining!");
			} else {
				totalCost = Integer.parseInt(moneyTextField.getText())
						- totalCost;
				moneyTextField.setText(Integer.toString(totalCost));
				runner.getGroup().setMoney(
						Integer.parseInt(moneyTextField.getText()));
				runner.getGroup().getWagon().getInventory().add(toBuy);
				runner.getGroup().getWagon().getInventory().clean();
				clearAfterBuy();
				System.out.println(runner.getGroup().getWagon().getInventory());
				System.out.println("MONEY " + runner.getGroup().getMoney());
			}
		}
	}
	/**
	 * Resets all text fields to blank after "Buy" button is pressed.
	 */
	private void clearAfterBuy()
	{
		bulletFieldEdit.setText("");
		clothingFieldEdit.setText("");
		gunFieldEdit.setText("");
		medicineFieldEdit.setText("");
		foodFieldEdit.setText("");
		wAxleFieldEdit.setText("");
		wWheelFieldEdit.setText("");
		wTongueFieldEdit.setText("");
		oxenFieldEdit.setText("");
		//updateBuy();
	}
	
	/**
	 * When "Buy" button is pressed, method checks to see if "price per item" text fields are blank,
	 * if they're not, "total price" is updated to reflect money spent.
	 */
	private void updateBuy()
	{
		String tempText = "";
		if (!(bulletFieldEdit.getText().equals("")))
		{
			try
			{
				if(Integer.parseInt(bulletFieldEdit.getText())<0)
				{
					bulletFieldEdit.setText("");
				}
				tempText += (Integer.parseInt(bulletFieldEdit.getText()) * Integer.parseInt(bulletFieldPer.getText()));
				bulletFieldStatic.setText(tempText);
				tempText ="";
			}
			catch(NumberFormatException nfe)
			{
				bulletFieldEdit.setText("");
			}
		}
		else
		{
			bulletFieldStatic.setText("0");
		}
		if (!(clothingFieldEdit.getText().equals("")))
		{
			try
			{
				if(Integer.parseInt(clothingFieldEdit.getText())<0)
				{
					clothingFieldEdit.setText("");
				}
				tempText += (Integer.parseInt(clothingFieldEdit.getText()) * Integer.parseInt(clothingFieldPer.getText()));
				clothingFieldStatic.setText(tempText);
				tempText ="";
			}
			catch(NumberFormatException ex)
			{
				clothingFieldEdit.setText("");
			}
		}
		else
		{
			clothingFieldStatic.setText("0");
		}
		if (!(foodFieldEdit.getText().equals("")))
		{
			try
			{
				if(Integer.parseInt(foodFieldEdit.getText())<0)
				{
					foodFieldEdit.setText("");
				}
				tempText += (Integer.parseInt(foodFieldEdit.getText()) * Integer.parseInt(foodFieldPer.getText()));
				foodFieldStatic.setText(tempText);
				tempText ="";
			}
			catch(NumberFormatException ex)
			{
				foodFieldEdit.setText("");
			}
		}
		else
		{
			foodFieldStatic.setText("0");
		}
		if (!(gunFieldEdit.getText().equals("")))
		{
			try
			{
				if(Integer.parseInt(gunFieldEdit.getText())<0)
				{
					gunFieldEdit.setText("");
				}
				tempText += (Integer.parseInt(gunFieldEdit.getText()) * Integer.parseInt(gunFieldPer.getText()));
				gunFieldStatic.setText(tempText);
				tempText ="";
			}
			catch(NumberFormatException ex)
			{
				gunFieldEdit.setText("");
			}
		}
		else
		{
			gunFieldStatic.setText("0");
		}
		if (!(medicineFieldEdit.getText().equals("")))
		{
			try
			{
				if(Integer.parseInt(medicineFieldEdit.getText())<0)
				{
					medicineFieldEdit.setText("");
				}
				tempText += (Integer.parseInt(medicineFieldEdit.getText()) * Integer.parseInt(medicineFieldPer.getText()));
				medicineFieldStatic.setText(tempText);
				tempText ="";
			}
			catch(NumberFormatException ex)
			{
				medicineFieldEdit.setText("");
			}
		}
		else
		{
			medicineFieldStatic.setText("0");
		}
		if (!(wAxleFieldEdit.getText().equals("")))
		{
			try
			{
				if(Integer.parseInt(wAxleFieldEdit.getText())<0)
				{
					wAxleFieldEdit.setText("");
				}
				tempText += (Integer.parseInt(wAxleFieldEdit.getText()) * Integer.parseInt(wAxleFieldPer.getText()));
				wAxleFieldStatic.setText(tempText);
				tempText ="";
			}
			catch(NumberFormatException ex)
			{
				wAxleFieldEdit.setText("");
			}
		}
		else
		{
			wAxleFieldStatic.setText("0");
		}
		if (!(wWheelFieldEdit.getText().equals("")))
		{
			try
			{if(Integer.parseInt(wWheelFieldEdit.getText())<0)
			{
				wWheelFieldEdit.setText("");
			}
				tempText += (Integer.parseInt(wWheelFieldEdit.getText()) * Integer.parseInt(wWheelFieldPer.getText()));
				wWheelFieldStatic.setText(tempText);
				tempText ="";
			}
			catch(NumberFormatException ex)
			{
				wWheelFieldEdit.setText("");
			}
		}
		else
		{
			wWheelFieldStatic.setText("0");
		}
		if (!(wTongueFieldEdit.getText().equals("")))
		{
			try
			{
				if(Integer.parseInt(wTongueFieldEdit.getText())<0)
				{
					wTongueFieldEdit.setText("");
				}
				tempText += (Integer.parseInt(wTongueFieldEdit.getText()) * Integer.parseInt(wTongueFieldPer.getText()));
				wTongueFieldStatic.setText(tempText);
				tempText ="";
			}
			catch(NumberFormatException ex)
			{
				wTongueFieldEdit.setText("");
			}
		}
		else
		{
			wTongueFieldStatic.setText("0");
		}
		if (!(oxenFieldEdit.getText().equals("")))
		{
			try
			{if(Integer.parseInt(oxenFieldEdit.getText())<0)
			{
				oxenFieldEdit.setText("");
			}
				tempText += (Integer.parseInt(oxenFieldEdit.getText()) * Integer.parseInt(oxenFieldPer.getText()));
				oxenFieldStatic.setText(tempText);
				tempText ="";
			}
			catch(NumberFormatException ex)
			{
				oxenFieldEdit.setText("");
			}
		}
		else
		{
			oxenFieldStatic.setText("0");
		}
	}
}
